﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Configuration;
using System.Xml.Linq;
using System.Windows.Shapes;
using System.Collections;

namespace TfsAlert.Modules.Header
{
    public class ConfigSettings : INotifyPropertyChanged
    {
        string _configurationFile = string.Empty;
        Hashtable keyValueSettings = null;
                
        public ConfigSettings()
        {
            _configurationFile = string.Format( @"{0}\{1}", Environment.CurrentDirectory, @"Modules\TfsAlert.Modules.Header.dll.config" );

            XDocument setttingsDocument = XDocument.Load( _configurationFile );
            XName[] elementName = { "add" };

            var settings = 
                from    decendents in setttingsDocument.Descendants()
                where   elementName.Contains( decendents.Name )
                select  new {
                            key = (string)decendents.Attribute( "key" ),
                            value = (string)decendents.Attribute( "value" )
                };

            keyValueSettings = new Hashtable( settings.Count() );

            foreach( var setting in settings )
            {
                keyValueSettings.Add( setting.key, setting.value );
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private string _todaysDate;
        public string TodaysDate 
        {
            get
            {
                if ( string.IsNullOrEmpty(_todaysDate) )
                    _todaysDate = DateTime.Now.ToShortDateString();

                return _todaysDate;
            }
            set
            {
                _todaysDate = value;
                OnPropertyChanged( "TodaysDate" );
            } 
        }

        private string _daysToReleaseDate;
        public string DaysToReleaseDate
        {
            get
            {
                if (  string.IsNullOrEmpty(_daysToReleaseDate) )
                {
                    DateTime releaseDate = DateTime.Parse( (string)this.keyValueSettings["ReleaseDate"] );
                    TimeSpan daysLeft = releaseDate.Date.Subtract( DateTime.Now );

                    _daysToReleaseDate = daysLeft.Days.ToString();
                }

                return _daysToReleaseDate;
            }
            set
            {
                _daysToReleaseDate = value;
                OnPropertyChanged( "DaysToReleaseDate" );
            }
        }

        private string _daysToCodeFreeze;
        public string DaysToCodeFreeze
        {
            get
            {
                if ( string.IsNullOrEmpty(_daysToCodeFreeze) )
                {
                    DateTime codeFreezeDate = DateTime.Parse( (string)this.keyValueSettings["CodeFreeze"] );
                    TimeSpan daysLeft = codeFreezeDate.Date.Subtract( DateTime.Now );

                    _daysToCodeFreeze = daysLeft.Days.ToString();
                }

                return _daysToCodeFreeze;
            }
            set
            {
                _daysToCodeFreeze = value;
                OnPropertyChanged( "DaysToCodeFreeze" );
            }
        }


        protected void OnPropertyChanged( string name )
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if ( handler != null )
                handler( this, new PropertyChangedEventArgs( name ) );
        }

        internal void ResetData()
        {
            this.DaysToCodeFreeze = string.Empty;
            this.DaysToReleaseDate = string.Empty;
            this.TodaysDate = string.Empty;
        }
    }
}
